﻿using _8Group.MES.IService;
using _8Group.MES.Meng_IService;
using _8Group.MES.Model.MD主数据;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using _8Group.MES.MengIService.DTO;

namespace _8Group.MES.Meng_Service
{
    [ApiExplorerSettings(GroupName ="孟祥宇")]
    /// <summary>
    /// 供应商服务
    /// </summary>
    public class MdVendorService : ApplicationService, IMdVendorService
    {
        /// <summary>
        /// 供应商表
        /// </summary>
        private readonly IRepository<MdVendor, int> _mdVendorRepository;
        public MdVendorService(IRepository<MdVendor, int> mdVendorRepository)
        {
            _mdVendorRepository = mdVendorRepository;
        }
        /// <summary>
        /// 获取供应商数据
        /// </summary>
        /// <param name="vendorCode"></param>
        /// <param name="vendorName"></param>
        /// <param name="vendorNick"></param>
        /// <returns></returns>
        public async Task<string> GetMdVendorListAsync(string ?vendorCode,string ?vendorName,string ?vendorNick)
        {
            var mdvendor = await _mdVendorRepository.GetListAsync();
            var mdvendors = mdvendor.AsQueryable();

            if (!string.IsNullOrEmpty(vendorCode))
            {
                mdvendors = mdvendors.Where(x => x.VendorCode.Contains(vendorCode));
            }
            if (!string.IsNullOrEmpty(vendorName))
            {
                mdvendors = mdvendors.Where(x => x.VendorName.Contains(vendorName));
            }
            if (!string.IsNullOrEmpty(vendorNick))
            {
                mdvendors = mdvendors.Where(x => x.VendorNick== vendorNick);
            }
            return JsonConvert.SerializeObject(new {code="0",data=mdvendors, count = mdvendors.Count(), msg = "success" }, Formatting.Indented);
        }
        /// <summary>
        /// 供应商新增
        /// </summary>
        /// <param name="mdvendor"></param>
        /// <returns></returns>
        public async Task<int> AddMdVendorAsync(MdVendorDTO mdvendor)
        {
            var n=AutoMapperExt.MapTo<MdVendor>(mdvendor);
            await _mdVendorRepository.InsertAsync(n);
            return 1;
        }
        /// <summary>
        /// 供应商删除
        /// </summary>
        /// <param name="mdvendor"></param>
        /// <returns></returns>
        public async Task<int> DeleteMdVendorAsync(int id)
        {
            await _mdVendorRepository.DeleteAsync(id,true);
            return 1;
        }
        /// <summary>
        /// 供应商修改
        /// </summary>
        /// <param name="mdvendor"></param>
        /// <returns></returns>
        public async Task<int> UpdateMdVendorAsync(UpdateMdVendorDTO mdvendor)
        {
            mdvendor.UpdateBy = "孟祥宇";
            mdvendor.UpdateTime = DateTime.Now;
            var n = AutoMapperExt.MapTo<MdVendor>(mdvendor);
            await _mdVendorRepository.UpdateAsync(n);
            return 1;
        }
    }
}
